package com.funo.logic.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.funo.logic.model.CompilationInParameter;
import com.funo.logic.model.LogicConfig;
import com.funo.logic.model.QueryResponse;
import com.funo.logic.model.RequestParameter;
import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class CompilationInParameterService extends AbstractService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public void resetRequestParameter(String arrangeId, List<RequestParameter> list) {
        CompilationInParameter compilationInParameter = this.getCompilationInParameter(arrangeId);
        compilationInParameter.setRequestParameterList(list);
        stringRedisTemplate.delete(getCacheKey(arrangeId));
        stringRedisTemplate.opsForValue().set(getCacheKey(arrangeId), JSONUtil.toJsonStr(compilationInParameter));
    }

    /**
     * 新增或者更新某个查询返回对象
     * @param arrangeId
     * @param queryResponse
     */
    public void addOrUpdateQueryResponse(String arrangeId, QueryResponse queryResponse) {
        CompilationInParameter compilationInParameter = this.getCompilationInParameter(arrangeId);
        if (CollectionUtil.isNotEmpty(compilationInParameter.getQueryResponseList())) {
            // 声明一个变量，用于判断是否已经存在
            boolean isExist = false;
            for (int i = 0; i < compilationInParameter.getQueryResponseList().size(); i++) {
                if (compilationInParameter.getQueryResponseList().get(i).getBelongNodeId().equals(queryResponse.getBelongNodeId())) {
                    compilationInParameter.getQueryResponseList().set(i, queryResponse);
                    isExist = true;
                    break;
                }
            }
            if (!isExist) {
                compilationInParameter.getQueryResponseList().add(queryResponse);
            }
        } else {
            compilationInParameter.setQueryResponseList(Lists.newArrayList(queryResponse));
        }
        stringRedisTemplate.delete(getCacheKey(arrangeId));
        stringRedisTemplate.opsForValue().set(getCacheKey(arrangeId), JSONUtil.toJsonStr(compilationInParameter));
    }

    /**
     * 新增/更新逻辑判断的返回全局变量
     * 逻辑判断的返回值为HandleResult, 所以QueryResponse的columnList只设置code和message
     * @param logicConfig 逻辑配置
     * @since 2024年7月19日16:53:05
     * @author cms
     */
    public void addOrUpdateLogicConfigResponse(String arrangeId, QueryResponse queryResponse) {
        CompilationInParameter compilationInParameter = this.getCompilationInParameter(arrangeId);
        if (CollectionUtil.isNotEmpty(compilationInParameter.getQueryResponseList())) {
            Optional<QueryResponse> optional = compilationInParameter.getQueryResponseList().stream().filter(qr -> qr.getBelongNodeId().equals(queryResponse.getBelongNodeId())).findFirst();
            if (!optional.isPresent()) {
                compilationInParameter.getQueryResponseList().add(queryResponse);
            }
        } else {
            compilationInParameter.setQueryResponseList(Lists.newArrayList(queryResponse));
        }
        stringRedisTemplate.delete(getCacheKey(arrangeId));
        stringRedisTemplate.opsForValue().set(getCacheKey(arrangeId), JSONUtil.toJsonStr(compilationInParameter));
    }

    public CompilationInParameter getCompilationInParameter(String arrangeId) {
       String value = stringRedisTemplate.opsForValue().get(getCacheKey(arrangeId));
       if (StringUtils.isNotEmpty(value)) {
           return JSONUtil.toBean(value, CompilationInParameter.class, true);
       }
       return new CompilationInParameter();
    }

    public String getCachePrefix() {
        return "compilationInParameter:";
    }
}
